/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.cicp;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.io.OutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author tangli
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-8-15 下午9:16:26          tangli         1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class ProbeWin extends JFrame {
    public ProbeWin() {
    }

    @Override
    protected void frameInit() {
        super.frameInit();
        this.setSize(600, 500);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel northPanel = new JPanel(new GridLayout(2, 2, 2, 10));
        northPanel.setBorder(BorderFactory.createTitledBorder("启动状态"));
        northPanel.add(new JLabel("采集器编号：" + ProbeEnv.probeId, SwingConstants.LEFT));
        //        northPanel.add(new JLabel("" + ProbeEnv.probeId, SwingConstants.LEFT));
        northPanel.add(new JLabel("采集器类型：" + ProbeEnv.probeType, SwingConstants.LEFT));
        //        northPanel.add(new JLabel("" + ProbeEnv.probeType, SwingConstants.LEFT));
        northPanel.add(new JLabel("采集器名称：" + ProbeEnv.probeName, SwingConstants.LEFT));
        //        northPanel.add(new JLabel("" + ProbeEnv.probeName, SwingConstants.LEFT));
        String startTime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());
        northPanel.add(new JLabel("启动时间：  " + startTime, SwingConstants.LEFT));
        //        northPanel.add(new JLabel(startTime, SwingConstants.LEFT));
        this.getContentPane().add(northPanel, BorderLayout.NORTH);

        JPanel centerPanel = new JPanel(new BorderLayout());
        centerPanel.setBorder(BorderFactory.createTitledBorder("系统日志"));
        JTextArea textArea = new JTextArea();
        textArea.setBackground(Color.black);
        textArea.setForeground(Color.white);
        textArea.setLineWrap(true);
        PrintStream out = new PrintStream(new GuiOutputStream(textArea));
        System.setOut(out);
        JScrollPane jsp = new JScrollPane(textArea);
        jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        centerPanel.add(jsp, BorderLayout.CENTER);
        this.getContentPane().add(centerPanel, BorderLayout.CENTER);

        this.setTitle("采集器");
    }

    /**
     * @param args
     * @throws UnsupportedLookAndFeelException 
     * @throws IllegalAccessException 
     * @throws InstantiationException 
     * @throws ClassNotFoundException 
     */
    public static void main(String[] args) throws Exception {
        if (System.getProperty("ibm.stream.nio") == null) {
            System.setProperty("ibm.stream.nio", "true");
        }

        /* 指定jDom解析器 */
        if (System.getProperty("javax.xml.parsers.SAXParserFactory") == null) {
            System.setProperty("javax.xml.parsers.SAXParserFactory", "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");
        }
        String laf = UIManager.getSystemLookAndFeelClassName();
        UIManager.setLookAndFeel(laf);
        UIManager.put("Label.font", new Font("宋体", Font.PLAIN, 14));
        ProbeEnv.initConfig();
        ProbeWin win = new ProbeWin();
        win.setVisible(true);
        new Startup().run();
    }

    class GuiOutputStream extends OutputStream {
        private final JTextArea _text;
        byte[] bytes = new byte[4096];
        int index = 0;
        int size = 0;
        int max_size = 10 * 1024;

        /** Creates a new instance of GuiOutputStream */
        public GuiOutputStream(JTextArea text) {
            _text = text;
        }

        @Override
        public void write(int i) {
            try {
                bytes[index] = (byte) i;
                if (i == '\n' || index == 4095) {
                    if (size >= max_size) {
                        _text.setText("");
                        size = 0;
                    }
                    size += (index + 1);
                    _text.append(new String(bytes, 0, index + 1, "UTF-8"));
                    index = 0;
                    _text.setCaretPosition(_text.getText().length());
                    _text.requestFocus();
                } else {
                    index++;
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}
